package com.xrit.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.xrit.constant.MessageConstant;
import com.xrit.exception.CustomException;
import com.xrit.pojo.CheckItem;
import com.xrit.query.QueryPageBean;
import com.xrit.result.PageResult;
import com.xrit.result.Result;
import com.xrit.service.CheckItemService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("checkItem")
public class CheckItemController {

    @Reference //远程订阅服务
    private CheckItemService checkItemService;

    @RequestMapping("/delete")
    public Result delete(Integer id) throws CustomException {
        int row = checkItemService.delete(id);
        if (row>0){
            return new Result(true, MessageConstant.DELETE_CHECKITEM_SUCCESS);
        }else{
            return new Result(false, MessageConstant.DELETE_CHECKITEM_FAIL);
        }
    }


    //新增
    @RequestMapping("/add")
    public Result add(@RequestBody CheckItem checkItem){
        int row = checkItemService.add(checkItem);
        if (row>0){
            return new Result(true, MessageConstant.ADD_CHECKITEM_SUCCESS);
        }else{
            return new Result(false, MessageConstant.ADD_CHECKITEM_FAIL);
        }
    }

    @RequestMapping("/findByPage")
    public Result findByPage(@RequestBody QueryPageBean queryPageBean){
        PageResult list = checkItemService.findByPage(queryPageBean);
        return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS,list);
    }

    @RequestMapping("/findAll")
    public Result findAll(){
        try{
            List<CheckItem> list = checkItemService.findAll();
            return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS,list);
        }catch (Exception e){
            e.printStackTrace();
        }
        return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);
    }

}
